package cn.airfei.aircore.testcore.controller;

import cn.airfei.aircore.testcore.api.StockApi;
import org.springframework.web.bind.annotation.*;
import cn.airfei.aircore.testcore.services.service.impl.StockServiceImpl;
import cn.airfei.aircore.testcore.storages.dto.StockDto;
import cn.airfei.aircore.testcore.storages.vo.StockVo;
import cn.airfei.aircore.core.common.Resp;
import cn.airfei.aircore.core.storage.PageParam;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import java.util.Map;
import java.util.List;
import lombok.extern.slf4j.Slf4j;
import javax.annotation.Resource;
import springfox.documentation.annotations.ApiIgnore;
import cn.airfei.aircore.core.enums.RespEnum;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 库存
 * </p>
 *
 * @author air
 * @version v1.0
 * @since 2022-04-25
 */
@RestController
@Slf4j
@RequestMapping("stock")
public class StockController implements StockApi {
    @Resource
    private StockServiceImpl stockService;


    /**
     * display a listing of the resource.
     *
     * @param stockDto
     * @param bindingResult
     * @param pageParam
     * @return
     */
    @Override
    @GetMapping(value = "")
    public Resp<Page<StockVo>> index(@Validated StockDto stockDto, BindingResult bindingResult, PageParam pageParam){
        if (bindingResult.hasErrors()){
            return Resp.response(RespEnum.PARAM_VALID_ERROR.getCode(), RespEnum.PARAM_VALID_ERROR.getMsg()+bindingResult.getFieldError().getDefaultMessage());
        }
        return Resp.response(stockService.index(stockDto, pageParam));
    }

    /**
     * 只查询单表数据，不关联任何表
     *
     * @param map
     * @return
     */
    @Override
    @GetMapping(value = "singleTablePage")
    public Resp<Page<StockVo>> singleTablePage(@ApiIgnore @RequestParam Map<String,Object> map){
        return Resp.response(stockService.singleTablePage(map));
    }

    /**
     * Display the specified resource.
     *
     * @param id
     * @return
     */
    @Override
    @GetMapping(value = "/{id}")
    public Resp<StockVo> show(@PathVariable("id") String id) {
        return Resp.response(stockService.show(id));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param stockDto
     * @return
     */
    @Override
    @PostMapping(value = "")
    public Resp<Integer> store(@RequestBody StockDto stockDto) {
        return Resp.response(stockService.store(stockDto));
    }

    /**
     * 支持批量删除，以英文逗号（,） 分割
     *
     * @param ids
     * @return
     */
    @Override
    @DeleteMapping(value = "/{ids}")
    public Resp<Integer> destroy(@PathVariable("ids") String ids) {
        return Resp.response(stockService.destroy(ids));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param stockDto
     * @return
     */
    @Override
    @PutMapping(value = "")
    public Resp<Integer> update(@RequestBody StockDto stockDto) {
        return Resp.response(stockService.update(stockDto));
    }

    /**
     * 设置Stock状态
     * @param id
     * @param status
     * @return
     */
    @Override
    @PutMapping(value = "setStatus")
    public Resp<Boolean> setStatus(Long id, Integer status) {
        return Resp.response(stockService.setStatus(id,status));
    }

    /**
     * get all resource in storage
     * @return
     */
    @Override
    @GetMapping(value = "getList")
    public Resp<List<StockVo>> getList(@ApiIgnore @RequestParam Map<String,Object> map) {
        return Resp.response(stockService.getList(map));
    }

    /**
     * 更新多条数据（主键id必传）
     *
     * @return
     */
    @Override
    @PostMapping(value = "updateBatch")
    public Resp<List<StockVo>> updateBatch(@RequestBody List<StockDto> stockDtoList) {
        return Resp.response(stockService.updateBatch(stockDtoList));
    }

    /**
     * 添加多条数据
     *
     * @return
     */
    @Override
    @PostMapping(value = "saveBatch")
    public Resp<List<StockVo>> saveBatch(@RequestBody List<StockDto> stockDtoList) {
        return Resp.response(stockService.saveBatch(stockDtoList));
    }

}
